package java8;

import java.util.*;

/**
 * @author lzy
 * Create DateTime: 2018/7/18 11:13
 * @version v1.0
 */
public class TestLamda {
    public static void main(String[] args) {
        // 匿名内部类
        Comparator<Integer> comparator = Integer::compare;
        // lambda表达式
        Comparator<Integer> comparator1 = Integer::compare;

        TreeSet<Integer> ts = new TreeSet<>(comparator1);
        List<Employee> employees = Arrays.asList(
                new Employee("张三", 18, 9999.99),
                new Employee("李四", 38, 5555.555),
                new Employee("王五", 50, 6666.66),
                new Employee("赵柳", 16, 3333.33),
                new Employee("田七", 8, 7777.77));

        // 需求：获取当前公司员工中年龄大于等于35岁的员工信息
        List<Employee> emps = filterEmployees(employees);
        emps.forEach(System.out::println);
        System.out.println("------------------");

        emps = filterEmployees2(employees);
        emps.forEach(System.out::println);
        System.out.println("------------------");

        emps = filterEmployee(employees, new FilterEmployeeByAge());
        emps.forEach(System.out::println);
        System.out.println("------------------");

        emps = filterEmployee(employees, new FilterEmployeeBySalary());
        emps.forEach(System.out::println);
        System.out.println("------------------");


        // 优化方式二：匿名内部类
        emps = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() < 20;
            }
        });
        emps.forEach(System.out::println);
        System.out.println("------------------");

        // 优化方式三: lambda表达式
        emps = filterEmployee(employees, employee -> employee.getAge() < 20);
        emps.forEach(System.out::println);
        System.out.println("------------------");

        // 优化方式四
        employees.stream().filter(e -> e.getSalary() >= 3000).limit(2).sorted((e1, e2) -> {
            if (e1.getAge() > e2.getAge()) {
                return 1;
            } else if (e1.getAge() == e2.getAge()) {
                return 0;
            } else {
                return -1;
            }
        }).map(Employee::getName).forEach(System.out::println);
        System.out.println("------------------");
    }

    // 需求：获取当前公司员工中年龄大于等于35岁的员工信息
    public static List<Employee> filterEmployees(List<Employee> employees) {
        List<Employee> result = new ArrayList<>();
        for (Employee employee :
                employees) {
            if (employee.getAge() >= 35) {
                result.add(employee);
            }
        }
        return result;
    }

    // 需求：获取当前公司员工中年龄大于等于35岁,工资大于5000的员工信息
    public static List<Employee> filterEmployees2(List<Employee> employees) {
        List<Employee> result = new ArrayList<>();
        for (Employee employee :
                employees) {
            if (employee.getAge() >= 35 && employee.getSalary() >= 5000) {
                result.add(employee);
            }
        }
        return result;
    }

    // 优化方式1：采用设计模式-策略设计模式
    public static List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp :
                list) {
            if (mp.test(emp)) {
                emps.add(emp);
            }
        }
        return emps;
    }


}
